<?php
include("../class/mysql.class.php");
include("../submit/Articles/get_list_article.php");

$obj = new Mysql ();
// hedhia stat 2


  //$r  = "select s.id_sortie,a.ref_article,a.photo_name,a.designation,a.prix,s.Qte,s.Date_cmd,s.id_user 
  //from st_attr_sortie s,st_article a where a.id_article = s.id_article ORDER BY s.Date_cmd DESC";
  //echo $r;
  if($_POST["id_article"]!=null && $_POST['dateD']==null && $_POST['dateF']==null){
    
    $r = "select 
              s.id_sortie,
              a.designation,
              a.ref_article,
              a.photo_name,
              a.prix,
              s.Qte,
              s.Date_cmd,
              s.id_user
              
    ";
    if(!empty($_POST["id_unite"])){
      $r .= " ,s.id_unite ";
    }    
    
    $r .="
    from st_attr_sortie s, 
     st_article a ";

    if(!empty($_POST["id_unite"])){
      $r .= " ,st_att_unite un ";
    }    
    
    $r .= "where s.id_article =".$_POST['id_article'];
       

    if(!empty($_POST["id_unite"])){
      $r .=" AND s.id_unite = ".$_POST["id_unite"]." AND s.id_unite = un.id_unite ";       
      //echo "<br> UNITE".$_POST["id_unite"]."<br>";       
    }


    $r .=" AND a.id_article = s.id_article  
    AND s.Qte <> 0  ORDER BY s.Date_cmd DESC";
    //echo "article isset<br>".$r; 
    
  }
  
  if($_POST['dateD']!=null && $_POST['dateF']!=null && $_POST["id_article"]==null){
    
    $dD = $_POST['dateD'];
    //$datD =explode('/', $dD);
    $dateD = $dD;//$datD['1'].'/'.$datD['0'].'/'.$datD['2'];

    $dF = $_POST['dateF'];
    //$datF =explode('/', $dF);
    $dateF = $dF;//$datF['1'].'/'.$datF['0'].'/'.$datF['2'];

    $r = "select 
                    s.id_sortie,
                    a.ref_article,
                    a.photo_name,
                    a.designation,
                    a.prix,s.Qte,
                    s.Date_cmd,s.id_user ";
      if(!empty($_POST["id_unite"])){
      $r .= " ,s.id_unite ";
    }        
    
    $r.= "from 
              st_attr_sortie s,
              st_article a";
    if(!empty($_POST["id_unite"])){
           $r .= " ,st_att_unite un ";
        }    
        $r.="
        where a.id_article = s.id_article AND s.Date_cmd >= '".$dateD."' AND s.Date_cmd <= '".$dateF."'";

     if(!empty($_POST["id_unite"])){
      $r .=" AND s.id_unite = ".$_POST["id_unite"]." AND s.id_unite = un.id_unite ";       
      //echo "<br> UNITE".$_POST["id_unite"]."<br>";       
    }   


     $r.=   " ORDER BY s.Date_cmd DESC";
    //echo "date isset<br>".$r; 
  }
  

  if($_POST['dateD']==null && $_POST['dateF']==null && $_POST["id_article"]==null){
    
    $r = "select s.id_sortie,a.ref_article,a.photo_name,a.designation,
                 a.prix,s.Qte,s.Date_cmd,s.id_user 
          from st_attr_sortie s,st_article a ";

          if(!empty($_POST["id_unite"])){
            $r .= " ,st_att_unite un ";
          }    

    $r.= " where a.id_article = s.id_article ";

      if(!empty($_POST["id_unite"])){
        $r .=" AND s.id_unite = ".$_POST["id_unite"]." AND s.id_unite = un.id_unite ";       
        //echo "<br> UNITE".$_POST["id_unite"]."<br>";       
      }

    $r.="  ORDER BY s.Date_cmd DESC";
    //echo "date isset<br>"; 
  }

  if($_POST['dateD']!=null && $_POST['dateF']!=null && $_POST["id_article"]!=null){

    $dD = $_POST['dateD'];
    //$datD =explode('/', $dD);
    $dateD = $dD;//$datD['1'].'/'.$datD['0'].'/'.$datD['2'];

    $dF = $_POST['dateF'];
    //$datF =explode('/', $dF);
    $dateF = $dF;//$datF['1'].'/'.$datF['0'].'/'.$datF['2'];

    
    //$r = "select s.id_sortie,a.ref_article,a.photo_name,a.designation,a.prix,s.Qte,s.Date_cmd,s.id_user from st_attr_sortie s,st_article a where a.id_article = s.id_article AND s.id_article=".$_POST['id_article']." AND s.Date_cmd >= '".$dateD."' AND s.Date_cmd <= '".$dateF."' ORDER BY s.Date_cmd DESC";
    //echo "date isset & article<br>"; 
    $r = "select 
              s.id_sortie,
              a.designation,
              a.ref_article,
              a.photo_name,
              a.prix,
              s.Qte,
              s.Date_cmd,
              s.id_user
    ";
    
    if(!empty($_POST["id_unite"])){
      $r .= " ,s.id_unite ";
    }    
    
    $r .="
    from st_attr_sortie s, 
     st_article a ";

    if(!empty($_POST["id_unite"])){
      $r .= " ,st_att_unite un ";
    }    
    
    $r .= "where s.id_article =".$_POST['id_article'];
       
    if(!empty($_POST["id_unite"])){
      $r .=" AND s.id_unite = ".$_POST["id_unite"];       
      //echo "<br> UNITE".$_POST["id_unite"]."<br>";       
    }

    
    if(!empty($_POST["id_unite"])){
      $r .=" AND s.id_unite = ".$_POST["id_unite"]." AND s.id_unite = un.id_unite ";       
      //echo "<br> UNITE".$_POST["id_unite"]."<br>";       
    }

    $r .=" AND a.id_article = s.id_article 
    AND s.Qte <> 0   AND s.Date_cmd >= '".$dateD."' AND s.Date_cmd <= '".$dateF."' ORDER BY s.Date_cmd DESC";
    //echo "article isset<br>".$r; 
    
  }
  $res = mysql_query($r);
  ?>
<div id="printablediv">
  <div id='container' style='width: 1000px; margin: 0 auto'>

    <table cellpadding="0" cellspacing="0" border="0" class="table table-striped table-bordered" id="example">
        <thead>
    <tr>
        <th text-align:left;width:50px;>#</th>
        <th text-align:left;width:50px;>Référence</th> 
        <th text-align:left;width:50px;>Designation</th>
        <th text-align:left;width:50px;>Unité de vente</th>
        <th text-align:left;width:50px;>Qte</th>        
        <th text-align:left;width:50px;>Prix Vente</th>
        <th text-align:left;width:50px;>Opération</th>
    </tr>
    </thead>
    <tbody>  
    <?php  
    $cpt = 1;
    $qtTotal = 0;
    while($row = mysql_fetch_object($res))
    {
      //print_r($row);
    ?>
    <tr>
        <td text-align:left;width:50px;><?php echo $cpt?></td>
        <td text-align:left;width:150px;><?php echo '<a class="fancybox-effects-a" href="'.$row->photo_name.'" target="_blank">'.$row->ref_article.'</a>' ?></td>
        <td text-align:left;width:350px;><?php echo $row->designation?></td>
        <td text-align:left;width:150px;>
           <?php 
            $table = "SELECT DISTINCT u.raison_social, u.id_unite FROM st_attr_sortie s,st_att_unite u
            where u.id_unite = s.id_unite AND s.id_sortie = ".$row->id_sortie;     
            //echo $table;
            $colonne = mysql_query($table);     
            while($data = mysql_fetch_array($colonne)){
              echo $data['raison_social'];    
            } 
          
          ?>
        </td>
        <td text-align:left;width:150px;><?php echo number_format($row->Qte, 0, '', ' ')?></td>
        <td text-align:left;width:150px;><?php echo $row->prix?></td>
        <td text-align:left;width:150px;><?php echo $row->Date_cmd;?></td>
        
    </tr>

    <?php 
    $qtTotal += $row->Qte;
    $cpt++;
    }
    ?>
    
  </table>

  <h4>Qt Total : <?php echo number_format($qtTotal, 0, '', ' ');?></h4>

  </div>
  </div>
  <input class="btn btn-primary btn-small" type="button" value="Imprimer" onclick="javascript:printDiv('printablediv')" style="padding: 6px 24px; margin-top: 20px;font-weight: bold;" />

  <?php

  include("../template/footer-ajax.php");
  ?>
  
  <script type="text/javascript">
          $.fn.dataTableExt.sErrMode = 'throw'; 
          $(".tablesorter").tablesorter(); 
  </script>